package com.mdz.nc.Controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.nc.Entity.Sbwhb;
import com.mdz.nc.Entity.Sbxxb;
import com.mdz.nc.Entity.SysNc;
import com.mdz.nc.Mapper.SbwhbMapper;
import com.mdz.nc.Mapper.SbxxbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2020-04-13
 */
@Controller
@RequestMapping("/nc/sbxxb")
public class SbxxbController {

    @Autowired
    private SbxxbMapper sbxxbMapper;
    @Autowired
    private SbwhbMapper sbwhbMapper;

    @RequestMapping("/sbxx")
    public String sbxx(ModelMap map){
        List<Sbxxb> sbxxb=sbxxbMapper.selectList(null);
        map.put("sbxxb",sbxxb);
        return "sbxx";
    }

    @RequestMapping("/sbwh/{sbbh}")
    public String sbwh(ModelMap map,@PathVariable("sbbh") String sbbh){
        List<Sbxxb> sbxxb=sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().like("sbbh",sbbh));
        map.put("sbxxb",sbxxb);
        List<Sbwhb> sbwhb=sbwhbMapper.getsbwh(sbbh);
        map.put("sbwhb",sbwhb);
        return "sbwh";
    }

    @ResponseBody
    @RequestMapping(value = "/getsbwh/{sbbh}",method = RequestMethod.GET)
    public List<Sbxxb> getsbwh(ModelMap map, @PathVariable("sbbh") String sbbh){
        List<Sbxxb> sbxxb=sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().like("sbbh",sbbh));
        return sbxxb;
    }

    @ResponseBody
    @RequestMapping(value = "/getsblist/{sbbh}",method = RequestMethod.GET)
    public String getsblist(HttpServletResponse response,@PathVariable("sbbh") String sbbh){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        List<Sbxxb> ncList=sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().like("sbbh",sbbh));
        List<Sbxxb> rows = ncList;
        int total = ncList.size();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/getsball",method = RequestMethod.GET)
    public String getsball(@RequestParam int pageNumber, @RequestParam int pageSize, HttpServletResponse response){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        List<Sbxxb> ncList=sbxxbMapper.selectList(null);
        PageInfo page=new PageInfo(ncList);
        List<SysNc> rows = page.getList();
        int total = (int) page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/editspkfk/{sbbh}", method = RequestMethod.PUT)
    public Integer editspkfk(Sbxxb sbxxb) {
        return sbxxbMapper.update(sbxxb, new UpdateWrapper<Sbxxb>().eq("sbbh", sbxxb.getSbbh()));
    }

    @ResponseBody
    @RequestMapping(value = "/maxsbbh/{sbbh}",method = RequestMethod.GET)
    public String maxsbbh(@PathVariable("sbbh") String sbbh){
        String maxsbbh=sbxxbMapper.maxsbbh(sbbh);
        return sbxxbMapper.maxsbbh(sbbh);
    }

    @ResponseBody
    @RequestMapping(value = "/insertsb",method = RequestMethod.POST)
    public int insertsb(Sbxxb sbxxb){
        System.out.println(sbxxb.getYpdx());
        System.out.println(sbxxb.getYpdx().length());
        String[] sjshoum = sbxxb.getYpdx().split(",");
        if (sbxxb.getYpdx().length()<=2){
            sbxxb.setYpdx("");
        }else {
        for (int i=0;i<sjshoum.length;i++){
            System.out.println("第"+i+"的值为:"+sjshoum[i]);
            System.out.println("第"+i+"的值的长度为:"+sjshoum[i].length());
            if(i==1&&sbxxb.getYpdx().length()>2){
                sbxxb.setYpdx(sjshoum[i-1]+"+"+sjshoum[i]);
            }if (i==2&&sbxxb.getYpdx().length()>2){
                sbxxb.setYpdx(sjshoum[i-2]+"+"+sjshoum[i-1]+"+"+sjshoum[i]);
            }if (i==0&&sbxxb.getYpdx().length()>2){
                sbxxb.setYpdx(sjshoum[i]);
            }
        }
//        if(sjshoum[1].length()==0&&sjshoum[2].length()==0){
//            sbxxb.setYpdx(sjshoum[0]);
//        }else if (sjshoum[0].length()==0&&sjshoum[1].length()==0&&sjshoum[2].length()==0){
//            sbxxb.setYpdx("");
//        }else if(sjshoum[2].length()==0){
//            sbxxb.setYpdx(sjshoum[0]+"+"+sjshoum[1]);
//        }else {
//            sbxxb.setYpdx(sjshoum[0]+"+"+sjshoum[1]+"+"+sjshoum[2]);
//        }



//        if (sbxxb.getYpdx().length()==9){
//            sbxxb.setYpdx(sjshoum[0]+sjshoum[1]);
//        }else if (sbxxb.getYpdx().length()==13){
//            sbxxb.setYpdx(sjshoum[0]+sjshoum[1]+"+"+sjshoum[2]+sjshoum[3]);
//        }else {
//            sbxxb.setYpdx(sjshoum[0]+sjshoum[1]+"+"+sjshoum[2]+sjshoum[3]+"+"+sjshoum[4]+sjshoum[5]);
        }
        return sbxxbMapper.insert(sbxxb);
    }

    @ResponseBody
    @RequestMapping(value = "/updatesbxxb/{sbbh01}", method = RequestMethod.PUT)
    public Integer updatesbxxb(Sbxxb sbxxb, @PathVariable("sbbh01") String sbbh01) {
        System.out.println(sbxxb);
        return sbxxbMapper.update(sbxxb, new UpdateWrapper<Sbxxb>().eq("sbbh", sbxxb.getSbbh()));
    }

    @GetMapping(value = "/index")
    public String index(HttpSession session, @RequestParam(value = "op",required = false, defaultValue = "") String op, Model model){
        System.out.println(op);
//        map.put("op",op);
//        request.setAttribute("op",op);
//        request.setAttribute("op",op);
//        model.addAttribute("op", op);
        session.setAttribute("op",op);
        return "sbxx";
    }

    @ResponseBody
    @GetMapping("/message")
    public String getMessage(HttpSession session){
//        model.addAttribute("abcd","123123123");
        session.setAttribute("message","HELLO WORLD");
//        request.setAttribute("message","HELLO WORLD");
        return "message";
    }

    @ResponseBody
    @RequestMapping(value = "/updatesbxxbbybf/{sbbh01}", method = RequestMethod.PUT)
    public Integer updatesbxxbbybf(Sbxxb sbxxb, @PathVariable("sbbh01") String sbbh01) {
        LocalDate today = LocalDate.now();
        sbxxb.setBfrq(today);
        sbxxb.setBfsm(sbxxb.getSbbh()+"设备在，"+today+"报废");
        sbxxb.setBfbs("1");
        return sbxxbMapper.update(sbxxb, new UpdateWrapper<Sbxxb>().eq("sbbh", sbxxb.getSbbh()));
    }

    @ResponseBody
    @RequestMapping(value = "/updatesbxxbbywh/{sbbh01}", method = RequestMethod.PUT)
    public Integer updatesbxxbbywh(Sbxxb sbxxb, @PathVariable("sbbh01") String sbbh01) {
        System.out.println(sbxxb.getBmid());
        sbxxb.setBmid(sbxxb.getBmid());
        sbxxb.setShiyr(sbxxb.getShiyr());
        return sbxxbMapper.update(sbxxb, new UpdateWrapper<Sbxxb>().eq("sbbh", sbxxb.getSbbh()));
    }

    @ResponseBody
    @RequestMapping(value = "/getbmid/{sbbh01}", method = RequestMethod.GET)
    public Integer getbmid(@PathVariable("sbbh01") String sbbh01){
        List<Sbxxb> sbxxbs=sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("sbbh",sbbh01));
        return sbxxbs.get(0).getBmid();
    }

    @ResponseBody
    @RequestMapping(value = "/getshiyr/{sbbh01}", method = RequestMethod.GET)
    public String getshiyr(@PathVariable("sbbh01") String sbbh01){
        List<Sbxxb> sbxxbs=sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("sbbh",sbbh01));
        return sbxxbs.get(0).getShiyr();
    }

}
